/**
 * Created by yangfan on 2016/9/21.
 */
define(['config/appregister',"../../services/httpServices","i18n","wangEditor"],function(app,httpService,i18n){
    app.directive("uiEditor",function(){
        return {
            scope:{
                content:"=setModel",
                config:"=editorType"
            },
            link:function(scope,element,attrs){
                //editor.createNewEditor()
                var id = attrs.id;
                //var head = getElementsByTagName("head");
                if(!id){
                    return
                }
                var editor = new wangEditor(id);
                var url = "http://101.37.82.219:9000/upload";
                if(typeof scope.config !='undefined'){
                    if(scope.config == 'tiny'){
                        editor.config.menus = [
                            '|',     // '|' 是菜单组的分割线
                            'bold',
                            'underline',
                            'italic',

                            'img'


                        ];
                    }
                }

                editor.config.uploadImgUrl =url;
                // editor.config.uploadImgFileName = "file";
                editor.config.uploadParams = {
                    // token1: 'abcde',
                    // token2: '12345'
                };
                editor.config.emotionsShow = 'value';
                editor.config.emotions = {
                    'default': {
                        title: '默认',
                        data: './emotions.data'
                    },
                    'weibo': {
                        title: '微博表情',
                        data: [
                            {
                                icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/7a/shenshou_thumb.gif',
                                value: '[草泥马]'
                            },
                            {
                                icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/60/horse2_thumb.gif',
                                value: '[神马]'
                            },
                            {
                                icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/bc/fuyun_thumb.gif',
                                value: '[浮云]'
                            },
                            {
                                icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/c9/geili_thumb.gif',
                                value: '[给力]'
                            },
                            {
                                icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/f2/wg_thumb.gif',
                                value: '[围观]'
                            },
                            {
                                icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/70/vw_thumb.gif',
                                value: '[威武]'
                            }
                        ]
                    }
                };
                editor.create();
                  //var curEditor=  editor.createNewEditor(id);
                if(typeof scope.content!="undefined"){
                   // curEditor.$txt.append(scope.content);
                   editor.$txt.html(scope.content);
                }
                //editor.onchange = function(){
                //    scope.content = editor.$txt.html();
                //}
                editor.onchange = function () {
                    // 编辑区域内容变化时，实时打印出当前内容
                    scope.$apply(function(){
                        scope.content = editor.$txt.html();
                    })
                };
            }
        }
    })
        .directive("insertImg",function(){
            return {
                templateUrl:"app/business/home/directives/templates/insertImage.html",
                link:function(scope,element,attrs){
                    scope.enterkey = function(){};
                    var id="testdiv";
                    scope.upload = function(){
                        var html = '<img src="them/face/01.gif">';
                        $("#"+id).focus();
                        insertImg(html);
                    };
                    scope.selectFile = function(){

                    };

                    function insertImg(str){

                        var selection= window.getSelection ? window.getSelection() : document.selection;

                        var range= selection.createRange ? selection.createRange() : selection.getRangeAt(0);

                        if (!window.getSelection){

                            document.getElementById(id).focus();

                            var selection= window.getSelection ? window.getSelection() : document.selection;

                            var range= selection.createRange ? selection.createRange() : selection.getRangeAt(0);

                            range.pasteHTML(str);

                            range.collapse(false);

                            range.select();

                        }else{

                            document.getElementById(id).focus();

                            range.collapse(false);

                            var hasR = range.createContextualFragment(str);

                            var hasR_lastChild = hasR.lastChild;

                            while (hasR_lastChild && hasR_lastChild.nodeName.toLowerCase() == "br" && hasR_lastChild.previousSibling && hasR_lastChild.previousSibling.nodeName.toLowerCase() == "br") {

                                var e = hasR_lastChild;

                                hasR_lastChild = hasR_lastChild.previousSibling;

                                hasR.removeChild(e)

                            }

                            range.insertNode(hasR);

                            if (hasR_lastChild) {

                                range.setEndAfter(hasR_lastChild);

                                range.setStartAfter(hasR_lastChild)

                            }

                            selection.removeAllRanges();

                            selection.addRange(range)

                        }

                    }

                }
            }
        })
});
